package com.example.smartcanteen.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartcanteen.mapper.Order1Mapper;
//import com.example.smartcanteen.mapper.OrderDetailsMapper;
import com.example.smartcanteen.mapper.ShopCartMapper;
import com.example.smartcanteen.model.*;
import com.example.smartcanteen.utils.ResponseMessage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private Order1Mapper order1Mapper;
//    @Autowired
//    private OrderDetailsMapper orderDetailsMapper;


    //查看购物车
    public ResponseMessage select(int studentID) {
        ResponseMessage responseMessage = new ResponseMessage();

        //通过学生ID查看购物车信息
        MPJLambdaWrapper<ShopCart> shopCartMPJLambdaWrapper = new MPJLambdaWrapper<>();
        shopCartMPJLambdaWrapper.selectAll(ShopCart.class)
                .select("t1.name as 'name'")
                .select("t2.shopname as 'shopname'")
                .select("t3.goodsname,t3.url,t3.price")
                .leftJoin(Student.class, Student::getId, ShopCart::getStudentID)
                .leftJoin(Shop.class, Shop::getId, ShopCart::getShopID)
                .leftJoin(Goods.class, Goods::getId, ShopCart::getGoodsID)
                .eq("studentID", studentID);
        List<ShopCartDTO> shopCartDTOS = shopCartMapper.selectJoinList(ShopCartDTO.class, shopCartMPJLambdaWrapper);
        responseMessage.setData(shopCartDTOS);
        return responseMessage;

    }


    //多表联查确定生成订单所需学生ID、支付方式、总金额、就餐形式（堂食、打包）
    public List<ShopCartDTO> getOrder1(int studentID) {
        MPJLambdaWrapper<ShopCart> shopCartMPJLambdaWrapper = new MPJLambdaWrapper<>();
        shopCartMPJLambdaWrapper.selectAll(ShopCart.class)
                .select("t1.price")
                .leftJoin(Goods.class, Goods::getId, ShopCart::getGoodsID)
                .eq("studentID", studentID);
        return shopCartMapper.selectJoinList(ShopCartDTO.class, shopCartMPJLambdaWrapper);
    }


    //购物车生成订单
    public ResponseMessage createOrder(int studentID, int shopID,int count, double price, String payment, String form,int goodsID) {
        ResponseMessage responseMessage = new ResponseMessage();

        //获取生成订单所需
//        List<ShopCartDTO> shopCartDTOS = getOrder1(studentID);

        //生成新订单并添加所需数据
        Order1 order1 = new Order1();
        order1.setStudentID(studentID);
        order1.setShopID(shopID);
        order1.setForm(form);
        order1.setPayment(payment);
        order1.setCount(count);
        order1.setPrice(price);
        order1.setGoodsID(goodsID);
        //计算订单总价
        double orderTotal = count * price;
//        for (ShopCartDTO shopCartDTO : shopCartDTOS) {
//            orderTotal += shopCartDTO.getPrice() * shopCartDTO.getCount();
//        }
        order1.setTotal(orderTotal);

        //获取当前时间
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(now);
        order1.setBuytime(time);

        //生成订单
        responseMessage.setSuccess(order1Mapper.insert(order1) != 0);

        //查找订单
        QueryWrapper<Order1> order1QueryWrapper = new QueryWrapper<>();
        order1QueryWrapper.eq("studentID", studentID)
                .eq("total", orderTotal)
                .eq("buytime", time);
        responseMessage.setData(order1Mapper.selectList(order1QueryWrapper));
        System.out.println(responseMessage.getData());
        //生成详单
//        createOrderDetails(studentID, sdf.format(now), shopCartDTOS);
        return responseMessage;
    }


    //生成详单
//    public void createOrderDetails(int studentID, String buytime, List<ShopCartDTO> shopCartDTOS) {
//
//        //先查找主订单信息
//        MPJLambdaWrapper<Order1> order1MPJLambdaWrapper = new MPJLambdaWrapper<>();
//        order1MPJLambdaWrapper.selectAll(Order1.class)
//                .eq("studentID", studentID)
//                .eq("buytime", buytime);
//        Order1 order1 = order1Mapper.selectJoinOne(Order1.class, order1MPJLambdaWrapper);
//
//        //根据主订单信息依次生成详单信息
//        for (ShopCartDTO shopCartDTO : shopCartDTOS) {
//            OrderDetails orderDetails = new OrderDetails();
//            orderDetails.setOrderID(order1.getId());
//            orderDetails.setGoodsID(shopCartDTO.getGoodsID());
//            orderDetails.setCount(shopCartDTO.getCount());
//            orderDetailsMapper.insert(orderDetails);
//        }
//    }


    //购物车新增商品
    public  ResponseMessage joinGoods(ShopCart shopCart){
        System.out.println(shopCart);
        ResponseMessage responseMessage = new ResponseMessage();
        if (shopCartMapper.insert(shopCart)==1){
            responseMessage.setSuccess(true);
        }else {
            responseMessage.setSuccess(false);
        }
        return responseMessage;
    }
}
